#ifndef PWM_SYSFS_H
#define PWM_SYSFS_H
#include <stdio.h>

typedef struct {
    int chip; // PWMn
    int channel; // PWMn_m
    char path[64];        // PWM sysfs 绝对路径
    FILE *period_fp;      // 保持打开以避免频繁 fopen/fclose
    FILE *duty_cycle_fp;  // 保持打开以避免频繁 fopen/fclose
    FILE *enable_fp;      // 保持打开以避免频繁 fopen/fclose
    FILE *polarity_fp;    // 保持打开以避免频繁 fopen/fclose
} pwm_t;

typedef enum {
    PWM_POLARITY_NORMAL = 0,
    PWM_POLARITY_INVERSED = 1
} pwm_polarity_t;

// 初始化并导出 PWM
int pwm_open(pwm_t *pwm, int chip, int channel);

// 设置 PWM 周期，单位纳秒
int pwm_set_period(pwm_t *pwm, unsigned int period_ns);

// 设置 PWM 占空比，单位纳秒
int pwm_set_duty_cycle(pwm_t *pwm, unsigned int duty_cycle_ns);

// 使能或禁用 PWM
int pwm_enable(pwm_t *pwm, int enable); // 1 to enable,

// 设置 PWM 极性
int pwnm_set_polarity(pwm_t *pwm, pwm_polarity_t polarity);

// 操作PWM
int pwm_operate(pwm_t *pwm, int period_ns, int duty_cycle_ns, pwm_polarity_t polarity);

// 关闭并 unexport
int pwm_close(pwm_t *pwm);

#endif // PWM_SYSFS_H
